// pages/diyOffer/diyOffer.js

var Define = require("../../lib/Define.js")

var _types = [
  { value: 0, title: "婚纱照", checked: true },
  { value: 1, title: "艺术照" },
  { value: 2, title: "情侣照" },
  { value: 3, title: "全家福" },
  { value: 4, title: "妈咪照" },
  { value: 5, title: "闺蜜照" }
]

var _dressList = [
  {value:0,title:"男女各3套",checked:true},
  { value: 1, title: "男女各4套"},
  { value: 2, title: "男女各5套" },
  { value: 3, title: "男女各6套" }
]

var _albumList = [
  {value:0,title:"1本",checked:true},
  { value: 1, title: "2本"},
  { value: 2, title: "3本" },
  { value: 3, title: "4本" }
]

var _photoFrameList = [
  { value: 0, title: "1张", checked: true },
  { value: 1, title: "2张" },
  { value: 2, title: "3张" },
  { value: 3, title: "4张" }
]

var _marryDateList = [
  { value: 0, title: "一个月内", checked: true },
  { value: 1, title: "三个月内" },
  { value: 2, title: "半年内" },
  { value: 3, title: "一年内" }
]

var _planDateList = [
  { value: 0, title: "春<4-5月>", checked: true },
  { value: 1, title: "夏<6-8月>" },
  { value: 2, title: "秋<9-11月>" },
  { value: 3, title: "冬<12-2月>" }
]

var _name = null
var _phone = null
var _wexin = null
var _sessionUser = null

Page({

  /**
   * 页面的初始数据
   */
  data: {
    types:[],
    dressList:[],
    albumList:[],
    photoFrameList:[],
    marryDateList:[],
    planDateList:[],
    sessionUser:{},
    isMarryType:true       //是否是结婚照
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    _sessionUser = Define.getStoredUserInfo()
    if(!_sessionUser){
      _sessionUser = {}
    }else{
      _phone = _sessionUser.phone
    }

    this.setData({
      types:_types,
      dressList:_dressList,
      albumList: _albumList,
      photoFrameList: _photoFrameList,
      marryDateList:_marryDateList,
      planDateList:_planDateList,
      sessionUser: _sessionUser
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },
  onTypesChange:function(e){
    //拍摄类别
    for(var index = 0 ; index < _types.length ; index++ ){
      var item = _types[index]
      item.checked = (index == e.detail.value)
    }

    this.setData({
      types: _types,
      isMarryType: (e.detail.value == 0)
    })
  },
  onDressListChange:function(e){
    //拍摄服装
    
    for (var index = 0; index < _dressList.length; index++) {
      var item = _dressList[index]
      item.checked = (index == e.detail.value)
    }

    this.setData({
      dressList: _dressList
    })
  },
  onAlbumListChange:function(e){
    //相册产品

    for (var index = 0; index < _albumList.length; index++) {
      var item = _albumList[index]
      item.checked = (index == e.detail.value)
    }

    this.setData({
      albumList: _albumList
    })
  },
  onPhotoFrameListChange:function(e){
    //放大产品
    for (var index = 0; index < _photoFrameList.length; index++) {
      var item = _photoFrameList[index]
      item.checked = (index == e.detail.value)
    }

    this.setData({
      photoFrameList: _photoFrameList
    })
  },
  onMarryDateListChange:function(e){
    //结婚日期
    for (var index = 0; index < _marryDateList.length; index++) {
      var item = _marryDateList[index]
      item.checked = (index == e.detail.value)
    }

    this.setData({
      marryDateList: _marryDateList
    })
  },
  onPlanDateListChange:function(e){
    //计划拍照日期
    for (var index = 0; index < _planDateList.length; index++) {
      var item = _planDateList[index]
      item.checked = (index == e.detail.value)
    }

    this.setData({
      planDateList: _planDateList
    })
  },
  bindNameInput:function(e){
    _name = e.detail.value
  },
  bindPhoneInput:function(e){
    _phone = e.detail.value
  },
  bindCodeInput:function(e){
    _code = e.detail.value
  },
  bindWexinInput:function(e){
    _wexin = e.detail.value
  },
  onSubmitBtnClick:function(){
    var errMsg = null
    
    if(!_name){
      errMsg = "请输入姓名"
    }

    if (!Define.checkMobile(_phone)) {
      errMsg = "请输入正确的电话号码"
    }

    if(errMsg){
      wx.showToast({
        title: errMsg,
        image: "/images/theme_d/toasterr.png",
        duration: 2000
      })
      return
    }

    var data = {}
    data.bid = Define.value.bid
    data.name = _name
    data.phone = _phone
    if(_wexin){
      data.weixin = _wexin
    }
    data.content = this.getContent()

    wx.showLoading({
      title: '加载中...',
    })

    wx.request({
      url: Define.value.baseUrl + "/app/user/addintention",
      data:data,
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      complete: function (res) {

        wx.hideLoading()

        if (res.data.code == 200) {
          wx.showModal({
            title: '提示',
            content: "报价成功，客服妹妹稍后会悄悄告诉你价格哦!",
            complete:function(){
              wx.navigateBack({
                
              })
            }
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.msg
          })
        }
      }
    })
    
  },
  getContent:function(){
    return this.getListCheckedValue("拍摄类别", _types) + ";" + this.getListCheckedValue("拍摄服装", _dressList) + ";" + this.getListCheckedValue("相册", _albumList) + ";" + this.getListCheckedValue("放大产品", _photoFrameList) + ";" + this.getListCheckedValue("结婚时间", _marryDateList) + ";" + this.getListCheckedValue("拍摄日期",_planDateList) + ";"
  },
  getListCheckedValue:function(title,list){
    for(var index = 0 ; index < list.length ; index++){
      var item = list[index]
      if(item.checked){
        return "["+title+":"+item.title+"]";
      }
    }

    return "";
  }
})